package com.hrt.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class SuperDao<T>  extends HibernateDaoSupport implements ISuperDao<T> {

	/* (non-Javadoc)
	 * @see com.hrt.util.ISuperDao#setSessionFactoryOverride(org.hibernate.SessionFactory)
	 */
	@Override
	@Autowired  
    public void setSessionFactoryOverride(SessionFactory sessionFactory)   
    {   
  
        super.setSessionFactory(sessionFactory);   
    } 
	
	/* (non-Javadoc)
	 * @see com.hrt.util.ISuperDao#queryByPage(java.lang.Class, int, int)
	 */
	@SuppressWarnings("deprecation")
	@Override
	public List<T> queryByPage(final Class<T> clz,final int pageIndex, final int pageSize)  {
		
	
		// TODO 
		@SuppressWarnings("unchecked")
		List<T> list=this.getHibernateTemplate().executeFind(new HibernateCallback<T>(){
			String hql="from "+clz.getName();
			@Override
			public T doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery(hql);
				query.setFirstResult((pageIndex-1)*pageSize);
				query.setMaxResults(pageSize);
				List<?> result=query.list();
				return  (T) result;
			}
			
			

		});
		return list;
	
	}
	
	
	/* (non-Javadoc)
	 * @see com.hrt.util.ISuperDao#queryById(T, java.lang.Integer)
	 */
	@Override
	public <T> T queryById(T clz,Long id)throws Exception
	{
		return this.getHibernateTemplate().get((Class<T>) clz, id);
	}
	
	/* (non-Javadoc)
	 * @see com.hrt.util.ISuperDao#save(java.lang.Object)
	 */
	@Override
	public  void  save(Object object)throws Exception
	{
		this.getHibernateTemplate().saveOrUpdate(object);
		
	}
	
	/* (non-Javadoc)
	 * @see com.hrt.util.ISuperDao#deleteById(java.lang.Class, java.lang.Long)
	 */
	@Override
	public  <T> void  deleteById(Class<T> cls, Long id)throws Exception
	{
	Object obj=this.getHibernateTemplate().load(cls, id);
		if(obj!=null)
		{
			getHibernateTemplate().delete(obj);
		}
		else{
			throw new Exception();
		}
		
	}
	
	/* (non-Javadoc)
	 * @see com.hrt.util.ISuperDao#getHibernateSessionFactory()
	 */
	@Override
	public SessionFactory getHibernateSessionFactory()throws Exception
	{
		
		return  this.getSessionFactory();
		
	}

	@Override
	public <T> void deleteByIds(Class clz, Long[] ids)  {
		
		String hql="from "+clz.getName()+" where ";
		List results = this.getHibernateTemplate().find(hql);
		
		for(int i=0;i<results.size();i++)
		{
			T object=(T) results.get(i);
			try {
				T newObject=(T) BeanUtils.cloneBean(object);
				Field field=object.getClass().getField("isdelete");
				field.setAccessible(true);
				field.setInt(object, 1);
				getHibernateTemplate().delete(object);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	
	
	
	

}
